package net.jefflnorton.gwt.sample.stockwatcher.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StockWatcher implements EntryPoint {

  private static final int REFRESH_INTERVAL = 5000;
  
  private VerticalPanel mainPanel = new VerticalPanel();
  private FlexTable stocksFlexTable = new FlexTable();
  private HorizontalPanel addPanel = new HorizontalPanel();
  private TextBox newSymbolTextBox = new TextBox();
  private Button addStockButton = new Button("Add");
  private Label lastUpdatedLabel = new Label();
  private ArrayList<String> stocks = new ArrayList<String>();

  /**
   * Entry point method.
   */
  public void onModuleLoad() {
	// Flex Table for Stock Data
	stocksFlexTable.setText(0, 0, "Symbol");
	stocksFlexTable.setText(0, 1, "Price");
    stocksFlexTable.setText(0, 2, "Change");
    stocksFlexTable.setText(0, 3, "Remove");
    
    // Horizontal Panel for Add Stock
    addPanel.add(newSymbolTextBox);
    addPanel.add(addStockButton);
    
    // Vertical Panel for Main Panel
    mainPanel.add(stocksFlexTable);
    mainPanel.add(addPanel);
    mainPanel.add(lastUpdatedLabel);
    
    // Associate Main Panel to HTML Page
    RootPanel.get("stockList").add(mainPanel);
    
    // Set Focus to New Symbol Input Box
    newSymbolTextBox.setFocus(true);
    
    Timer refreshTimer = new Timer() {
    	@Override
    	public void run() {
    		refreshWatchList();
    	}
    };
    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);
    
    // Add Stock Button Click Handler
    addStockButton.addClickHandler(new ClickHandler() {
    	public void onClick(ClickEvent event) {
    		addStock();
    	}
    });
    
    // New Symbol Text Box KeyPress Handler
    newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
    	public void onKeyPress(KeyPressEvent event) {
    		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
    			addStock();
    		}
    	}
    });

  }

/**
   * Add Stock to Flex Table
   */
  private void addStock() {
	  final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
	  newSymbolTextBox.setFocus(true);
	  
	  // Stock Code must be between 1 and 10 chars that are numbers, letters, or dots.
	  if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
		  Window.alert("'" + symbol + "' is not a valid symbol.");
		  newSymbolTextBox.selectAll();
		  return;
	  }
	  
	  newSymbolTextBox.setText("");
	  
	  // Don't add Stock Code if it already exists.
	  if (stocks.contains(symbol))
		  return;
	  
	  // Add Stock Code to Flex Table
	  int row = stocksFlexTable.getRowCount();
	  stocks.add(symbol);
	  stocksFlexTable.setText(row, 0, symbol);
	  
	  // Add Button to Remove Stock Code from Flex Table
	  Button removeStockButton = new Button("x");
	  removeStockButton.addClickHandler(new ClickHandler() {
		  public void onClick(ClickEvent event) {
			  int removedIndex = stocks.indexOf(symbol);
			  stocks.remove(removedIndex);
			  stocksFlexTable.removeRow(removedIndex + 1);
		  }
	  });
	  stocksFlexTable.setWidget(row, 3, removeStockButton);
	  
	  // Get Stock Price
	  refreshWatchList();
  }
  
  private void refreshWatchList() {
	  // TODO Auto-generated method stub
	
  }

}